<?php
namespace Android\Model;


class TeachplanModel extends BaseModel {


    /**
     * 根据教师id，获取该教师当前学期课程计划
     *
     * @param int $teacher_id   教师 id
     * @param int $term_id      学期 id
     */
    public function getTeachplanArr ($teacher_id,$term_id) {

        $result = $this->table("__TEACHPLAN__ plan, __TEACHER__ teacher, __COURSE__ course")
                       ->where("plan.teacher_id = {$teacher_id} AND plan.term_id = {$term_id} AND plan.teacher_id = teacher.id AND plan.course_id = course.id")
                       ->field("plan.*, teacher.name AS teacher_name, course.name AS course_name")
                       ->select();

        return $result;

    }


    public function get($id) {

        $result = $this->table("__TEACHPLAN__ plan, __TEACHER__ teacher, __COURSE__ course")
                       ->where("plan.id = {$id} AND plan.teacher_id = teacher.id AND plan.course_id = course.id")
                       ->field("plan.*, teacher.name AS teacher_name, course.name AS course_name")
                       ->find();
        return $result;
    }


    public function getTeachplanIdsByTid ($teacher_id , $term_id) {
        $sql = "SELECT id FROM __TEACHPLAN__ WHERE teacher_id = {$teacher_id} AND term_id = {$term_id}";
        $res = $this->query($sql);
        return $res;
    }

    /**
     * 根据教学计划，获取学生id数组
     *
     * @param $teachplan_id
     * @return array
     */
    public function getStudentIds ($teachplan_id) {

        $idsStr = $this->where('id = '.$teachplan_id)->getField('student_ids');
        if (empty($idsStr))
            return null;

        return explode(',',$idsStr);
    }

}